package com.toy.abash.service.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import com.toy.abash.dao.UpOrDownDAO;
import com.toy.abash.domain.UpOrDown;
import com.toy.abash.service.AbashManager;
import com.toy.abash.service.UpOrDownManager;
import com.toy.core.cache.annotation.CacheEvict;
import com.toy.core.cache.annotation.CachePackage;
import com.toy.core.dao.MongoCrudRepository;
import com.toy.core.service.impl.BaseManagerImpl;

@CachePackage(UpOrDown.class)
@Service("upOrDownManager")
public class UpOrDownManagerImpl extends BaseManagerImpl<UpOrDown> implements
		UpOrDownManager {
	
	@Autowired
	private UpOrDownDAO upOrDownDAO;
	
	@Autowired
	private AbashManager abashManager;
	
	@CacheEvict
	public UpOrDown saveUpOrDown(UpOrDown entity){
		entity.setOptDate(new Date());
		entity = upOrDownDAO.save(entity);
		Update update = new Update();
		if ( entity.isToUp()){
			update.inc("toUpNum", 1).inc("hotOrder", 1);
		}
		if ( entity.isToDown()){
			update.inc("toDownNum", 1).inc("hotOrder", -1);
		}
		abashManager.findAndModify(entity.getAbashId(), update);
		return entity;
	}
	
	@Override
	public MongoCrudRepository<UpOrDown,Long> getMongoCrudRepository() {
		return upOrDownDAO;
	}

}
